package errorx

import (
	"fmt"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

const (
	// 用户10001-20000
	ERROR_DEFAULT                        = 10001
	ERROR_REUQEST_PARAM_ERROR            = 10002
	ERROR_EXIST_TAG                      = 10003
	ERROR_NOT_EXIST_PARAME_X_USER_ID     = 10004
	ERROR_REDIS_CONNECT_EXCEPTION        = 10005
	ERROR_NOT_LOGIN                      = 10006
	ERROR_NOT_ACCESS_AUTHORITY           = 10007
	ERROR_DB_CONNECT_EXCEPTION           = 10011
	ERROR_DB_UPDATE_AFFECTED_ZERO        = 10012
	ERROR_ELASTIC_INIT_CLIENT_FAILED     = 10013
	ERROR_ELASTIC_INIT_FAILED            = 10014
	ERROR_ELASTIC_DOCUMENT_FAILED        = 10015 // deprecated
	ERROR_ELASTIC_DELETE_DOCUMENT_FAILED = 10016
	ERROR_EXCEL_WRITE_FAILED             = 10017

	ERROR_USERNAME_PASSWORD_NOT_EMPTY = 10101
	ERROR_FIND_USER_EXCEPTION         = 10102
	ERROR_USERNAME_PASSWORD_INCORRECT = 10103
	ERROR_TOKEN_EXPIRE                = 10104
	ERROR_GENERATE_TOKEN_EXCEPTION    = 10105

	ERROR_USERNAME_REQUIRED           = 10201
	ERROR_GENERATE_PASSWORD_EXCEPTION = 10202
	ERROR_USERNAME_EXISTS             = 10203
	ERROR_USER_ID_REQUIRED            = 10204
	ERROR_USER_STATUS_INVALID         = 10205

	// ERROR_OS_NAME_REQUIRED                         = 10211
	// ERROR_OS_NAME_EXISTS                           = 10212
	ERROR_OS_ID_REQUIRED                       = 10213
	ERROR_OS_STATUS_INVALID                    = 10214
	ERROR_MENU_NAME_REQUIRED                   = 10221
	ERROR_MENU_NAME_EXISTS                     = 10222
	ERROR_MENU_ID_REQUIRED                     = 10223
	ERROR_MENU_STATUS_INVALID                  = 10224
	ERROR_ROLE_NAME_REQUIRED                   = 10231
	ERROR_ROLE_NAME_EXISTS                     = 10232
	ERROR_ROLE_ID_REQUIRED                     = 10233
	ERROR_ROLE_STATUS_INVALID                  = 10234
	ERROR_PERM_NAME_REQUIRED                   = 10241
	ERROR_PERM_NAME_EXISTS                     = 10242
	ERROR_PERM_ID_REQUIRED                     = 10243
	ERROR_PERM_STATUS_INVALID                  = 10244
	ERROR_MEMBER_LEVEL_NAME_REQUIRED           = 10251
	ERROR_MEMBER_LEVEL_NAME_EXISTS             = 10252
	ERROR_MEMBER_LEVEL_ID_REQUIRED             = 10253
	ERROR_MEMBER_LEVEL_ID_INVALID              = 10254
	ERROR_BLOG_TERM_NAME_REQUIRED              = 10301
	ERROR_BLOG_TERM_NAME_EXISTS                = 10302
	ERROR_BLOG_TERM_ID_REQUIRED                = 10303
	ERROR_BLOG_TERM_ID_INVALID                 = 10304
	ERROR_BLOG_TERM_DELETE_DEFAULT_EXCEPTION   = 10305
	ERROR_BLOG_POST_TITLE_REQUIRED             = 10331
	ERROR_BLOG_POST_TITLE_EXISTS               = 10332
	ERROR_BLOG_POST_ID_REQUIRED                = 10333
	ERROR_BLOG_POST_ID_INVALID                 = 10334
	ERROR_BLOG_POST_STATUS_INVALID             = 10335
	ERROR_BLOG_POST_SEARCH_FAILED              = 10336
	ERROR_BLOG_PAGE_TITLE_REQUIRED             = 10341
	ERROR_BLOG_PAGE_TITLE_EXISTS               = 10342
	ERROR_BLOG_PAGE_ID_REQUIRED                = 10343
	ERROR_BLOG_PAGE_ID_INVALID                 = 10344
	ERROR_BLOG_PAGE_STATUS_INVALID             = 10345
	ERROR_KBPOST_TITLE_REQUIRED                = 10361
	ERROR_KBPOST_TITLE_EXISTS                  = 10362
	ERROR_KBPOST_ID_REQUIRED                   = 10363
	ERROR_KBPOST_ID_INVALID                    = 10364
	ERROR_KBPOST_STATUS_INVALID                = 10365
	ERROR_KBPOST_CONTENT_REQUIRED              = 10366
	ERROR_KBPOST_CONTENT_INVALID               = 10367
	ERROR_KBPOST_SECTION_ID_REQUIRED           = 10368
	ERROR_ATTACHMENT_TERM_NAME_REQUIRED        = 10401
	ERROR_ATTACHMENT_TERM_NAME_EXISTS          = 10402
	ERROR_ATTACHMENT_TERM_ID_REQUIRED          = 10403
	ERROR_ATTACHMENT_TERM_ID_INVALID           = 10404
	ERROR_ATTACHMENT_FILE_TYPE_NOT_ALLOWED     = 10431
	ERROR_ATTACHMENT_FILE_EMPTY                = 10432
	ERROR_ATTACHMENT_DIRECTORY_PARENT_REQUIRED = 10441
	ERROR_ATTACHMENT_DIRECTORY_NAME_REQUIRED   = 10442
	ERROR_ATTACHMENT_DIRECTORY_NAME_EXISTS     = 10443
	ERROR_ATTACHMENT_TITLE_REQUIRED            = 10444
	ERROR_ATTACHMENT_ID_REQUIRED               = 10445
	ERROR_ATTACHMENT_FILE_REQUIRED             = 10451
	ERROR_ATTACHMENT_FILE_SAVE_FAILED          = 10452
	ERROR_ATTACHMENT_FILE_SYNC_FAILED          = 10453

	ERROR_FORUM_TOPIC_ID_REQUIRED      = 10501
	ERROR_FORUM_TOPIC_ID_INVALID       = 10502
	ERROR_FORUM_POST_ID_REQUIRED       = 10511
	ERROR_FORUM_POST_ID_INVALID        = 10512
	ERROR_FORUM_POST_TITLE_REQUIRED    = 10513
	ERROR_FORUM_POST_CONTENT_REQUIRED  = 10514
	ERROR_FORUM_POST_PARENTID_REQUIRED = 10515
	ERROR_FORUM_POST_USER_ID_REQUIRED  = 10516
	ERROR_FORUM_POST_CONTENT_INVALID   = 10517
	ERROR_FORUM_USER_NICKNAME_REQUIRED = 10518

	ERROR_PRODUCT_TITLE_REQUIRED = 10601
	ERROR_PRODUCT_TITLE_EXISTS   = 10602
	ERROR_PRODUCT_ID_REQUIRED    = 10603
	ERROR_PRODUCT_ID_INVALID     = 10604
	ERROR_PRODUCT_STATUS_INVALID = 10605
	ERROR_PRODUCT_CODE_REQUIRED  = 10606

	ERROR_ORDER_ID_REQUIRED         = 10701
	ERROR_ORDER_ID_INVALID          = 10702
	ERROR_ORDER_PARAM_INVALID       = 10703
	ERROR_ORDER_TRADE_STATE_INVALID = 10704

	ERROR_BACKEMD_ERP_ITEM_ID_REQUIRED       = 10801
	ERROR_BACKEMD_ERP_QUANTITY_REQUIRED      = 10802
	ERROR_BACKEMD_ERP_SHOPNICK_REQUIRED      = 10803
	ERROR_BACKEMD_ERP_SKU_ID_REQUIRED        = 10804
	ERROR_BACKEMD_ERP_DELIVERY_CODE_REQUIRED = 10805 // deprecated
	ERROR_BACKEMD_ERP_DELIVERY_NAME_REQUIRED = 10806
	ERROR_BACKEMD_ERP_TRADE_ID_REQUIRED      = 10807 // deprecated
	ERROR_BACKEMD_ERP_WAYBILL_REQUIRED       = 10808 // deprecated

	ERROR_BACKEMD_INTEGRAL_TASK_RULW_NAME_REQUIRED = 10820

	ERROR_USER_AUTH_PARAM_INVALID = 11001
	ERROR_USER_EXISTS             = 11002
	ERROR_USER_REGISTER_FAILED    = 11003
)

var statusText = map[uint32]string{
	ERROR_DEFAULT:                        "服务器开小差啦，稍后再来试一试",
	ERROR_REUQEST_PARAM_ERROR:            "参数错误",            // ?
	ERROR_EXIST_TAG:                      "已存在该标签名称",        // ?
	ERROR_NOT_EXIST_PARAME_X_USER_ID:     "缺少必要参数x-user-id", // #24.04.27# 去除
	ERROR_REDIS_CONNECT_EXCEPTION:        "用户：%s, 获取redis连接异常",
	ERROR_NOT_LOGIN:                      "用户未登录",
	ERROR_NOT_ACCESS_AUTHORITY:           "沒有路径访问权限，请联系管理员",
	ERROR_DB_CONNECT_EXCEPTION:           "数据库繁忙，请稍后再试",
	ERROR_DB_UPDATE_AFFECTED_ZERO:        "更新数据影响行数为0",
	ERROR_ELASTIC_INIT_CLIENT_FAILED:     "全文搜索初始化失败-初始化客户端错误",
	ERROR_ELASTIC_INIT_FAILED:            "全文搜索初始化失败",
	ERROR_ELASTIC_DOCUMENT_FAILED:        "全文搜索创建文档失败",    // #25.06.27# 去除
	ERROR_ELASTIC_DELETE_DOCUMENT_FAILED: "全文搜索删除文档失败",
	ERROR_EXCEL_WRITE_FAILED:             "EXCEL文档写入失败",

	ERROR_USERNAME_PASSWORD_NOT_EMPTY: "用户名或密码不能为空",    // #24.04.27# 去除
	ERROR_FIND_USER_EXCEPTION:         "查询用户失败",        // ?
	ERROR_USERNAME_PASSWORD_INCORRECT: "用户名或密码错误",      // #24.04.27# 去除
	ERROR_TOKEN_EXPIRE:                "token失效，请重新登录", // ?
	ERROR_GENERATE_TOKEN_EXCEPTION:    "生成token失败",

	ERROR_USERNAME_REQUIRED:           "用户名是必填项",
	ERROR_GENERATE_PASSWORD_EXCEPTION: "生成初始密码失败",
	ERROR_USERNAME_EXISTS:             "用户名已存在",
	ERROR_USER_ID_REQUIRED:            "缺少必要参数user_id",
	ERROR_USER_STATUS_INVALID:         "该用户已注销或被禁用",

	// ERROR_OS_NAME_REQUIRED:                        "部门或职位名称是必填项",
	// ERROR_OS_NAME_EXISTS:                          "部门或职位名称已存在",
	ERROR_OS_ID_REQUIRED:                       "缺少必要参数os_id",
	ERROR_OS_STATUS_INVALID:                    "状态错误os_status",
	ERROR_MENU_NAME_REQUIRED:                   "菜单名是必填项",
	ERROR_MENU_NAME_EXISTS:                     "菜单名已存在",
	ERROR_MENU_ID_REQUIRED:                     "缺少必要参数menu_id",
	ERROR_MENU_STATUS_INVALID:                  "菜单状态错误",
	ERROR_ROLE_NAME_REQUIRED:                   "角色名是必填项",
	ERROR_ROLE_NAME_EXISTS:                     "角色名已存在",
	ERROR_ROLE_ID_REQUIRED:                     "缺少必要参数role_id",
	ERROR_ROLE_STATUS_INVALID:                  "角色状态错误",
	ERROR_PERM_NAME_REQUIRED:                   "权限名是必填项",
	ERROR_PERM_NAME_EXISTS:                     "权限名已存在",
	ERROR_PERM_ID_REQUIRED:                     "缺少必要参数perm_id",
	ERROR_PERM_STATUS_INVALID:                  "权限状态错误",
	ERROR_BLOG_TERM_NAME_REQUIRED:              "分类名及别名是必填项",
	ERROR_BLOG_TERM_NAME_EXISTS:                "分类名及别名的组合已存在",
	ERROR_BLOG_TERM_ID_REQUIRED:                "缺少必要参数term_id",
	ERROR_BLOG_TERM_ID_INVALID:                 "分类不存在",
	ERROR_BLOG_TERM_DELETE_DEFAULT_EXCEPTION:   "默认分类不能被删除",
	ERROR_BLOG_POST_TITLE_REQUIRED:             "文章标题是必填项",
	ERROR_BLOG_POST_TITLE_EXISTS:               "文章标题已存在",
	ERROR_BLOG_POST_ID_REQUIRED:                "缺少必要参数post_id",
	ERROR_BLOG_POST_ID_INVALID:                 "文章不存在",
	ERROR_BLOG_POST_STATUS_INVALID:             "文章状态错误",
	ERROR_BLOG_POST_SEARCH_FAILED:              "文章搜索失败",
	ERROR_BLOG_PAGE_TITLE_REQUIRED:             "页面标题是必填项",
	ERROR_BLOG_PAGE_TITLE_EXISTS:               "页面标题已存在",
	ERROR_BLOG_PAGE_ID_REQUIRED:                "缺少必要参数page_id",
	ERROR_BLOG_PAGE_ID_INVALID:                 "页面不存在",
	ERROR_BLOG_PAGE_STATUS_INVALID:             "页面状态错误",
	ERROR_KBPOST_TITLE_REQUIRED:                "文章标题是必填项",
	ERROR_KBPOST_TITLE_EXISTS:                  "文章标题已存在",
	ERROR_KBPOST_ID_REQUIRED:                   "缺少必要参数id",
	ERROR_KBPOST_ID_INVALID:                    "文章不存在",
	ERROR_KBPOST_STATUS_INVALID:                "文章状态错误",
	ERROR_KBPOST_CONTENT_REQUIRED:              "缺少必要参数文章内容",
	ERROR_KBPOST_CONTENT_INVALID:               "文章内容错误",
	ERROR_KBPOST_SECTION_ID_REQUIRED:           "缺少必要参数section_id",
	ERROR_ATTACHMENT_TERM_NAME_REQUIRED:        "分类名是必填项",
	ERROR_ATTACHMENT_TERM_NAME_EXISTS:          "分类名已存在",
	ERROR_ATTACHMENT_TERM_ID_REQUIRED:          "缺少必要参数term_id",
	ERROR_ATTACHMENT_TERM_ID_INVALID:           "分类不存在",
	ERROR_ATTACHMENT_FILE_TYPE_NOT_ALLOWED:     "抱歉，您无权上传此文件类型。", // Sorry, you are not allowed to upload this file type. || File format not allowed.
	ERROR_ATTACHMENT_FILE_EMPTY:                "文件为空，请选择其他文件。",  // 'This file is empty. Please try another.'
	ERROR_ATTACHMENT_DIRECTORY_PARENT_REQUIRED: "父文件夹是必须项",
	ERROR_ATTACHMENT_DIRECTORY_NAME_REQUIRED:   "文件夹路径是必填项",
	ERROR_ATTACHMENT_DIRECTORY_NAME_EXISTS:     "父文件夹下已存在相同路径",
	ERROR_ATTACHMENT_TITLE_REQUIRED:            "文件名称是必须项",
	ERROR_ATTACHMENT_ID_REQUIRED:               "缺少必要参数id",
	ERROR_ATTACHMENT_FILE_REQUIRED:             "获取文件失败",
	ERROR_ATTACHMENT_FILE_SAVE_FAILED:          "保存文件失败",
	ERROR_ATTACHMENT_FILE_SYNC_FAILED:          "上传文件失败",

	ERROR_FORUM_TOPIC_ID_REQUIRED:      "缺少必要参数id",
	ERROR_FORUM_TOPIC_ID_INVALID:       "记录不存在",
	ERROR_FORUM_POST_ID_REQUIRED:       "缺少必要参数id",
	ERROR_FORUM_POST_ID_INVALID:        "记录不存在",
	ERROR_FORUM_POST_TITLE_REQUIRED:    "缺少必要参数title",
	ERROR_FORUM_POST_CONTENT_REQUIRED:  "缺少必要参数content",
	ERROR_FORUM_POST_PARENTID_REQUIRED: "缺少必要参数parentId",
	ERROR_FORUM_POST_USER_ID_REQUIRED:  "缺少必要参数userId",
	ERROR_FORUM_POST_CONTENT_INVALID:   "文章内容有敏感词",
	ERROR_FORUM_USER_NICKNAME_REQUIRED: "缺少必要参数nickName",

	ERROR_PRODUCT_TITLE_REQUIRED: "产品名是必填项",
	ERROR_PRODUCT_TITLE_EXISTS:   "产品名已存在",
	ERROR_PRODUCT_ID_REQUIRED:    "缺少必要参数product_id",
	ERROR_PRODUCT_ID_INVALID:     "产品不存在",
	ERROR_PRODUCT_STATUS_INVALID: "产品状态错误",
	ERROR_PRODUCT_CODE_REQUIRED:  "产品编号是必填项",

	ERROR_ORDER_ID_REQUIRED:         "缺少必要参数order_id",
	ERROR_ORDER_ID_INVALID:          "订单不存在",
	ERROR_ORDER_PARAM_INVALID:       "订单参数错误",
	ERROR_ORDER_TRADE_STATE_INVALID: "订单状态错误",

	ERROR_BACKEMD_ERP_ITEM_ID_REQUIRED:       "缺少必要参数item_id",
	ERROR_BACKEMD_ERP_QUANTITY_REQUIRED:      "缺少必要参数quantity",
	ERROR_BACKEMD_ERP_SHOPNICK_REQUIRED:      "缺少必要参数shop_nick",
	ERROR_BACKEMD_ERP_SKU_ID_REQUIRED:        "缺少必要参数sku_id",
	ERROR_BACKEMD_ERP_DELIVERY_CODE_REQUIRED: "缺少必要参数delivery_code",
	ERROR_BACKEMD_ERP_DELIVERY_NAME_REQUIRED: "缺少必要参数delivery_name",
	ERROR_BACKEMD_ERP_TRADE_ID_REQUIRED:      "缺少必要参数trade_id",
	ERROR_BACKEMD_ERP_WAYBILL_REQUIRED:       "缺少必要参数waybill",

	ERROR_BACKEMD_INTEGRAL_TASK_RULW_NAME_REQUIRED: "缺少必要参数integral_task",

	ERROR_USER_AUTH_PARAM_INVALID: "登录参数错误",
	ERROR_USER_EXISTS:             "该用户已存在",
	ERROR_USER_REGISTER_FAILED:    "注册失败",
}

// GetMsg returns a text for the HTTP status code. It returns the empty
// string if the code is unknown.
func GetMsg(code uint32, withDefault bool) string { //////
	if msg, ok := statusText[code]; ok {
		return msg
	}

	if withDefault {
		return "The server is currently busy. Please try again later."
		// return "服务器开小差啦，稍后再来试一试"
	}

	return ""
}

// 错误返回的json
func Error(code uint32, msg string, args ...interface{}) error {
	if msg == "" {
		msg = GetMsg(code, true)
	}

	if len(args) > 0 {
		msg = fmt.Sprintf(msg, args...)
	}

	return NewCodeError(code, msg)
}

func Status(code uint32, msg string, args ...interface{}) error {
	if msg == "" {
		msg = GetMsg(code, true)
	}

	if len(args) > 0 {
		msg = fmt.Sprintf(msg, args...)
	}

	return status.Error(codes.Code(code), msg)
}
